package nl.harmidi.util;

import java.awt.Color;

public class ColorFunctions {

	/**
	 * Gives back the gradual <code>Color</code> in between 2 given
	 * <code>Color</code>s.
	 * 
	 * @param startColor
	 * @param endColor
	 * @param percentage
	 * @return
	 */
	public static Color getGradientColoring(Color startColor, Color endColor, long percentage) {

		int red = getNewColorValue(startColor.getRed(), endColor.getRed(), percentage);
		int green = getNewColorValue(startColor.getGreen(), endColor.getGreen(), percentage);
		int blue = getNewColorValue(startColor.getBlue(), endColor.getBlue(), percentage);

		Color newColor = new Color(red, green, blue);

		return newColor;
	}

	// get's the int value in between 2 other values, based on a percentage
	private static int getNewColorValue(int startColorValue, int endColorValue, long percentage) {
		if (percentage == 0L) {
			return startColorValue;
		} else {
			if (startColorValue > endColorValue) {
				// swap percentage
				percentage = 100 - percentage;
			}
			double percentageDouble = percentage / 100D;
			int maxMinMin = Math.max(endColorValue, startColorValue) - Math.min(endColorValue, startColorValue);

			return (int) ((percentageDouble * maxMinMin) + Math.min(startColorValue, endColorValue));
		}
	}

}
